/*
 * @(#)ListWorkflowRecordAction.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.workflow.web;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.MtmWfStageWfStep;
import com.dcivision.workflow.bean.WorkflowGroup;
import com.dcivision.workflow.bean.WorkflowRecord;
import com.dcivision.workflow.bean.WorkflowScheduleTriggers;
import com.dcivision.workflow.dao.MtmWfStageWfStepDAObject;
import com.dcivision.workflow.dao.WorkflowGroupDAObject;
import com.dcivision.workflow.dao.WorkflowRecordDAObject;
import com.dcivision.workflow.dao.WorkflowScheduleTriggersDAObject;
import com.dcivision.workflow.dao.WorkflowStepDAObject;

/**
 * <p>
 * Class Name: ListWorkflowRecordAction.java
 * </p>
 * <p>
 * Description: The list action class for ListWorkflowRecord.jsp
 * </p>
 * 
 * @author Angus Shiu
 * @company DCIVision Limited
 * @creation date 30/07/2003
 * @version $Revision: 1.14 $
 */
public class ListWorkflowStageAction extends AbstractListAction {

	public static final String REVISION = "$Revision: 1.14 $";

	/** Creates a new instance of ListWorkflowRecordAction */
	public ListWorkflowStageAction() {
		super();
		this.setListName("workflowStageList");
		this.setListFunctionName("getWorkflowStageList");
	}

	/**
	 * getFunctionCode
	 * 
	 * Abstract function which sub-class should implement to return the
	 * corresponding function code.
	 * 
	 * @return The function code
	 */
	public String getFunctionCode() {
		return (SystemFunctionConstant.WORKFLOW_RECORD);
	}

	/**
	 * getMajorDAOClassName
	 * 
	 * Abstract function which sub-class should implement to return the
	 * corresponding major DAO class name used in this class.
	 * 
	 * @return The DAO class name
	 */
	public String getMajorDAOClassName() {
		return ("com.dcivision.workflow.dao.WorkflowStageDAObject");
	}

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws ServletException {
		
		ListWorkflowStageForm StageForm = (ListWorkflowStageForm) form;
		String action = StageForm.getAction();
		//add by sars
	    if(StageForm.getFlag()!=null&&StageForm.getFlag().equals("reset"))
	    {
		    ListWorkflowStageForm tempStageForm = (ListWorkflowStageForm)request.getSession().getAttribute(this.getListName());
		    tempStageForm.setBasicSearchKeyword("");
			request.getSession().setAttribute(this.getListName(), tempStageForm);
			StageForm.setFlag(null);
	    }
	    if(StageForm.getFlag()!=null&&StageForm.getFlag().equals("stage"))
	    {
		    ListWorkflowStageForm tempStageForm = (ListWorkflowStageForm)request.getSession().getAttribute(this.getListName());
			request.getSession().setAttribute(this.getListName(), tempStageForm);
			StageForm.setFlag(null);
	    }
		if (action != null && action.equalsIgnoreCase("searchStage")) {
			request.getSession().setAttribute("oldSearchForm", StageForm);
			StageForm.setAction(null);
		}
		if (action != null && action.equalsIgnoreCase("return_searchStage")) {
			form=(ActionForm)request.getSession().getAttribute("oldSearchForm");	    
			StageForm.setWorkflowGroupID(((ListWorkflowStageForm)form).getWorkflowGroupID());
			StageForm.setWorkflowRecordID(((ListWorkflowStageForm)form).getWorkflowRecordID());
			StageForm.setBasicSearchField(((ListWorkflowStageForm)form).getBasicSearchField());
			StageForm.setBasicSearchType(((ListWorkflowStageForm)form).getBasicSearchType());
			StageForm.setBasicSearchKeyword(((ListWorkflowStageForm)form).getBasicSearchKeyword());
			StageForm.setAction(null);
      request.getSession().removeAttribute("oldSearchForm");
		}
		ListWorkflowStageForm listWorkflowStageForm = (ListWorkflowStageForm)form;
		if (Utility.isEmpty(listWorkflowStageForm.getWorkflowRecordID())
				&& !Utility.isEmpty(request.getParameter("ID"))) {
			listWorkflowStageForm.setWorkflowRecordID(request.getParameter("ID"));
		}
		if (Utility.isEmpty(listWorkflowStageForm.getWorkflowGroupID())
				&& !Utility.isEmpty(request.getParameter("WorkflowGroupID"))) {
			listWorkflowStageForm.setWorkflowGroupID(request
					.getParameter("WorkflowGroupID"));
		}
		
		AbstractSearchForm searchForm = (AbstractSearchForm)form;
		log.info(this.getClass().getName() + ", OP Mode:" + searchForm.getOpMode()
				+ ", Nav Mode:" + searchForm.getNavMode());
		// Set the function code for current action.
		ActionForward forward = this.retrieveFunctionCode(request, response,
				mapping);
		if (forward != null) {
			return forward;
		}
		// added by nancy
		this.canNew(form, request);
		// end
		return super.execute(mapping, form, request, response);

	}

	public void getListData(ActionMapping mapping, AbstractSearchForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws ApplicationException {
		SessionContainer sessionContainer = this.getSessionContainer(request);
		Connection conn = this.getConnection(request);
		ListWorkflowStageForm workflowSearchForm = (ListWorkflowStageForm) form;
		WorkflowRecordDAObject workflowRecordDAO = new WorkflowRecordDAObject(
				sessionContainer, conn);
		List workflowRecordList = new ArrayList();
		if (!Utility.isEmpty(workflowSearchForm.getWorkflowGroupID())) {
			workflowRecordList = workflowRecordDAO
					.getListByWorkflowGroupID(new Integer(workflowSearchForm
							.getWorkflowGroupID()));
		} else {
			workflowRecordList = workflowRecordDAO.getFullList();
		}
    //simon_add_start
    WorkflowGroupDAObject wfGroupDAO = new WorkflowGroupDAObject(sessionContainer, conn);
    WorkflowGroup wfGroup = (WorkflowGroup) wfGroupDAO.getObjectByID(TextUtility.parseIntegerObj(workflowSearchForm.getWorkflowGroupID()));
    request.setAttribute("workflowGroup", wfGroup);
    //simon_add_end
		request.setAttribute("workflowRecordList", workflowRecordList);
		super.getListData(mapping, form, request, response);
	}

	public void deleteListData(ActionMapping mapping, AbstractSearchForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws ApplicationException {

		String[] selectedID = form.getBasicSelectedID();
		super.deleteListData(mapping, form, request, response);
		// Cancel the Schedule Jobs
		for (int i = 0; i < selectedID.length; i++) {
			if (!Utility.isEmpty(selectedID[i])) {
				// Delete schedule job when a workflow record is deleted
				WorkflowScheduleTriggersDAObject triggerDAO = new WorkflowScheduleTriggersDAObject(
						this.getSessionContainer(request), this.getConnection(request));
				List triggerList = triggerDAO.getListByWorkflowRecordID(new Integer(
						selectedID[i]));
				for (int j = 0; j < triggerList.size(); j++) {
					WorkflowScheduleTriggers trigger = (WorkflowScheduleTriggers) triggerList
							.get(j);
				}
				// added by nancy
				MtmWfStageWfStepDAObject mtmStageStepDAO = new MtmWfStageWfStepDAObject(
						this.getSessionContainer(request), this.getConnection(request));
				List stageList = mtmStageStepDAO.getListByWorkflowStageID(new Integer(
						selectedID[i]));
				for (int j = 0; j < stageList.size(); j++) {
					MtmWfStageWfStep tempStage = (MtmWfStageWfStep) stageList.get(j);
					mtmStageStepDAO.deleteObject(tempStage);
				}
				this.canNew(form, request);
				// end

			}
		}

	}

	private void canNew(ActionForm form, HttpServletRequest request)
			throws NumberFormatException, ApplicationException {
		AbstractSearchForm searchForm = (AbstractSearchForm) form;
		ListWorkflowStageForm listWorkflowStageForm = (ListWorkflowStageForm) searchForm;
		String groupName = null;
		String recordName = null;
		if (!Utility.isEmpty(listWorkflowStageForm.getWorkflowRecordID())) {
			WorkflowRecordDAObject wfrDAO = new WorkflowRecordDAObject(this
					.getSessionContainer(request), this.getConnection(request));
			recordName = ((WorkflowRecord) wfrDAO.getObjectByID(Integer
					.valueOf(listWorkflowStageForm.getWorkflowRecordID())))
					.getWorkflowCode();
			if (Utility.isEmpty(listWorkflowStageForm.getWorkflowGroupID())) {
				listWorkflowStageForm.setWorkflowGroupID((((WorkflowRecord) wfrDAO
						.getObjectByID(Integer.valueOf(listWorkflowStageForm
								.getWorkflowRecordID()))).getWorkflowGroupID()).toString());
			}
		}
		if (!Utility.isEmpty(listWorkflowStageForm.getWorkflowGroupID())) {
			WorkflowGroupDAObject wfgDAO = new WorkflowGroupDAObject(this
					.getSessionContainer(request), this.getConnection(request));
			groupName = ((WorkflowGroup) wfgDAO.getObjectByID(Integer
					.valueOf(listWorkflowStageForm.getWorkflowGroupID()))).getGroupName();
		}
		request.setAttribute("groupName", groupName);
		if (!Utility.isEmpty(listWorkflowStageForm.getWorkflowRecordID())) {

			request.setAttribute("recordName", recordName);
			MtmWfStageWfStepDAObject mtmStageStepDAO = new MtmWfStageWfStepDAObject(
					this.getSessionContainer(request), this.getConnection(request));
			WorkflowStepDAObject wfStepDAO = new WorkflowStepDAObject(this
					.getSessionContainer(request), this.getConnection(request));
			Integer stepNum = new Integer(wfStepDAO.getListByWorkflowRecordID(
					new Integer(listWorkflowStageForm.getWorkflowRecordID())).size());
			Integer stageStepNum = new Integer(mtmStageStepDAO
					.getListByWorkflowRecordID(
							new Integer(listWorkflowStageForm.getWorkflowRecordID())).size());
			if (!stepNum.equals(stageStepNum)) {
				request.setAttribute("canNew", "Y");
			}
		}
	}

}
